package com.loper7.base.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;

import com.loper7.base.R;

import java.io.Serializable;

/**
 * @author LOPER7
 * @date 2017/10/24 9:59
 * @Description: ${todo}(生活不止眼前的苟且)
 */

public class BackHelper {

    private Activity mActivity;
    private Fragment mFragment;
    private Context mContext;
    private Intent mIntent;

    public BackHelper(Activity activity) {
        this.mActivity = activity;
    }

    public BackHelper(Fragment mFragment) {
        this.mFragment = mFragment;

    }

    public BackHelper(Context mContext) {
        this.mContext = mContext;
    }

    public Context getContext() {
        if (mFragment != null) {
            return mFragment.getActivity();
        } else {
            return mActivity;
        }
    }

    public IntentConfig builder() {
        return new IntentConfig();
    }

    public class IntentConfig {

        public IntentConfig() {
            mIntent = new Intent();
        }

        public IntentConfig setCls(Class cls) {
            if (mFragment != null) {
                mIntent.setClass(mFragment.getActivity(), cls);
            } else if (mActivity != null) {
                mIntent.setClass(mActivity, cls);
            } else {
                mIntent.setClass(mContext, cls);
            }
            return this;
        }

        public IntentConfig setCls(String str) {
            try {
                if (mFragment != null) {
                    mIntent.setClass(mFragment.getActivity(), Class.forName(str));
                } else if (mActivity != null) {
                    mIntent.setClass(mActivity, Class.forName(str));
                } else {
                    mIntent.setClass(mContext, Class.forName(str));
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            return this;
        }

        public IntentConfig addParams(String key, Object value) {
            if (value instanceof String) {
                mIntent.putExtra(key, (String) value);
            } else if (value instanceof Integer) {
                mIntent.putExtra(key, (Integer) value);
            } else if (value instanceof Serializable) {
                mIntent.putExtra(key, (Serializable) value);
            } else if (value instanceof Long) {
                mIntent.putExtra(key, (Long) value);
            } else if (value instanceof Float) {
                mIntent.putExtra(key, (Float) value);
            } else if (value instanceof Double) {
                mIntent.putExtra(key, (Double) value);
            } else if (value instanceof Parcelable[]) {
                mIntent.putExtra(key, (Parcelable[]) value);
            }
            return this;
        }

        public IntentConfig addFlag(int flag) {
            mIntent.addFlags(flag);
            return this;
        }

        public BackHelper build() {
            return BackHelper.this;
        }
    }

    /**
     * 开始导航
     */
    public void forward() {
        if (mIntent == null)
            return;
        if (mFragment != null) {
            KeyboardUtil.closeKeyboard(mFragment.getActivity());
            mFragment.startActivity(mIntent);
        } else if (mActivity != null) {
            KeyboardUtil.closeKeyboard(mActivity);
            mActivity.startActivity(mIntent);
        } else {
            mContext.startActivity(mIntent);
        }
    }

    /**
     * 开始导航
     *
     * @param requestCode 不能为-1
     */
    public void forward(int requestCode) {
        if (mIntent == null)
            return;
        if (mFragment != null) {
            KeyboardUtil.closeKeyboard(mFragment.getActivity());
            mFragment.startActivityForResult(mIntent, requestCode);
        } else if (mActivity != null) {
            KeyboardUtil.closeKeyboard(mActivity);
            mActivity.startActivityForResult(mIntent, requestCode);
        }
    }

    /**
     * 跳转到下一个 Activity，不销毁当前 Activity
     *
     * @param cls 下一个 Activity 的 Class
     */
    public void forward(Class<?> cls) {
        builder().setCls(cls).build().forward();
    }


    /**
     * 跳转到下一个 Activity，不销毁当前 Activity
     *
     * @param cls         下一个 Activity 的 Class
     * @param requestCode 请求码
     */
    public void forward(Class<?> cls, int requestCode) {
        builder().setCls(cls).build().forward(requestCode);
    }

    /**
     * 跳转到下一个 Activity，销毁当前 Activity
     *
     * @param intent 下一个 Activity 的意图对象
     */
    public void forwardAndFinish(Intent intent) {
        forward(intent);
        mActivity.finish();
    }

    /**
     * 跳转到下一个 Activity,不销毁当前 Activity
     *
     * @param intent 下一个 Activity 的意图对象
     */
    public void forward(Intent intent) {
        if (mFragment != null)
            KeyboardUtil.closeKeyboard(mFragment.getActivity());
        else
            KeyboardUtil.closeKeyboard(mActivity);

        if (mFragment != null)
            mFragment.startActivity(intent);
        else
            mActivity.startActivity(intent);
    }

    /**
     * 跳转到下一个 Activity,不销毁当前 Activity
     *
     * @param intent      下一个 Activity 的意图对象
     * @param requestCode 请求码
     */
    public void forward(Intent intent, int requestCode) {
        if (mFragment != null)
            KeyboardUtil.closeKeyboard(mFragment.getActivity());
        else
            KeyboardUtil.closeKeyboard(mActivity);
        if (mFragment != null)
            mFragment.startActivityForResult(intent, requestCode);
        else
            mActivity.startActivityForResult(intent, requestCode);
    }

    /**
     * 跳转到下一个 Activity，并且销毁当前 Activity
     *
     * @param cls 下一个 Activity 的 Class
     */
    public void forwardAndFinish(Class<?> cls) {
        builder().setCls(cls).build().forward();
        mActivity.finish();
    }

    /**
     * 执行跳转到下一个 Activity 的动画
     */
    public static void executeForwardAnim(Activity activity) {
        activity.overridePendingTransition(R.anim.bga_sbl_activity_forward_enter, R.anim.bga_sbl_activity_forward_exit);
    }

    /**
     * 执行回到到上一个 Activity 的动画
     */
    public static void executeBackwardAnim(Activity activity) {
        activity.overridePendingTransition(R.anim.bga_sbl_activity_backward_enter, R.anim.bga_sbl_activity_backward_exit);
    }

    /**
     * 退出当前页面
     */
    public void backward() {
        if (mFragment != null) {
            KeyboardUtil.closeKeyboard(mFragment.getActivity());
            mFragment.getActivity().finish();
        } else if (mActivity != null) {
            KeyboardUtil.closeKeyboard(mActivity);
            mActivity.finish();
        }
    }

}
